package com.changanw.tienchin.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.utils.SecurityUtils;
import com.changanw.tienchin.course.domain.Course;
import com.changanw.tienchin.course.mapper.CourseMapper;
import com.changanw.tienchin.course.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author changanw
 * @since 2023-11-23
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
  @Autowired
  CourseMapper courseMapper;
  @Override
  public List<Course> selectCourseList(Course course) {
    return courseMapper.selectCourseList(course);
  }

  @Override
  public AjaxResult addCourse(Course course) {
    course.setCreateBy(SecurityUtils.getUsername());
    course.setCreateTime(LocalDateTime.now());
    course.setDelFlag(0);
    return save(course) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
  }

  @Override
  public AjaxResult updateCourse(Course course) {
    course.setUpdateBy(SecurityUtils.getUsername());
    course.setUpdateTime(LocalDateTime.now());
    //防止这三个属性被修改
    course.setCreateTime(null);
    course.setCreateBy(null);
    course.setDelFlag(null);
    return updateById(course) ? AjaxResult.success("更新成功"):AjaxResult.error("更新失败");
  }

  @Override
  public boolean deleteCourseByIds(Long[] courseIds) {
    UpdateWrapper<Course> uw = new UpdateWrapper<>();
    uw.lambda().set(Course::getDelFlag,1).in(Course::getCourseId,courseIds);
    return update(uw);
  }

  @Override
  public AjaxResult getCourseByCourseType(Integer courseType) {
    QueryWrapper<Course> qw = new QueryWrapper<>();
    qw.lambda().eq(Course::getType,courseType);
    List<Course> list = list(qw);
    return AjaxResult.success(list);
  }

}
